using System;
using System.Collections.Generic;
using IQIGame.Onigao.GamePlay;
using LFloatMath.Math;
using NetProtocol.POD;

namespace IQIGame.Onigao.Logic
{
    public class BattleTeamController : TController
    {
        private BattlePlayController mPlayCtrl;
        private Dictionary<int, BattleTeam> m_Teams = new Dictionary<int, BattleTeam>();
        public Dictionary<int, BattleTeam> teams => this.m_Teams;

        public Queue<BattleTeamPOD> monsterQueue = new Queue<BattleTeamPOD>();
        public List<int> allMonsterTeam = new List<int>();
        public void Initialize(BattlePlayController rPlayCtrl, CreateBattlePOD rResp)
        {
            this.mPlayCtrl = rPlayCtrl;
            this.AddHeroTeam(rResp.redTeams[0]);

            if (rResp.blueTeams.Count <= 0)
            {
                throw new Exception("至少要有一个怪物队伍");
            }

            for (int i = 0; i < rResp.blueTeams.Count; i++)
            {
                this.monsterQueue.Enqueue(rResp.blueTeams[i]);
                this.allMonsterTeam.Add(rResp.blueTeams[i].monsterTeamID);
            }
            if (this.monsterQueue.TryDequeue(out var rMonsterTeam))
            {
                this.AddMonsterTeam(rMonsterTeam);
            }
        }

        public bool NextMonsterTeam()
        {
            if (this.m_Teams.TryGetValue(BattleConst.TeamBlueIndex, out var rTeam))
            {
                for (int i = 0; i < rTeam.units.Count; i++)
                {
                    var rUnit = rTeam.units[i];
                    this.mPlayCtrl.unitCtrl.RemoveUnit(rUnit);
                }

                rTeam.units.Clear();
            }

            if (this.monsterQueue.TryDequeue(out var nMonsterTeam))
            {
                this.AddMonsterTeam(nMonsterTeam);

                this.mPlayCtrl.queueCtrl.RefreshQueue();
                this.mPlayCtrl.startRound = this.mPlayCtrl.realRound;
                this.mPlayCtrl.battle.NextWave(this.teams[BattleConst.TeamBlueIndex]);
                return true;
            }

            return false;
        }

        protected override void OnDispose()
        {
            this.teams.Clear();
        }

        private void AddHeroTeam(BattleTeamPOD rHeroBattleTeam)
        {
            var nTeamIndex = BattleConst.TeamRedIndex;
            var rTeam = new BattleTeam().Initialize(nTeamIndex);
            this.m_Teams[nTeamIndex] = rTeam;
            
            for (int i = 0; i < rHeroBattleTeam.units.Count; i++)
            {
                var rUnit = UnitFactory.CreateBattleUnit(this.mPlayCtrl.battle, nTeamIndex, rHeroBattleTeam.units[i], false);
                this.mPlayCtrl.unitCtrl.AddUnit(rUnit);
            }
            rTeam.subtitute = rHeroBattleTeam.substitute;
        }

        private void AddMonsterTeam(BattleTeamPOD rMonsterBattleTeam)
        {
            var nTeamIndex = BattleConst.TeamBlueIndex;
            this.m_Teams[nTeamIndex] = new BattleTeam().Initialize(nTeamIndex);
            
            var rBatttle = this.mPlayCtrl.battle;
            for (int i = 0; i < rMonsterBattleTeam.units.Count; i++)
            {
                var rMonster = rMonsterBattleTeam.units[i];
                if (rMonster != null)
                {
                    var rUnit = UnitFactory.CreateBattleUnit(rBatttle,rMonsterBattleTeam.teamIndex, rMonster, true);
                    this.mPlayCtrl.unitCtrl.AddUnit(rUnit);
                }
            }
        }
    }
}